package com.uflower.user.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import com.uflower.user.interceptor.LoginUserInterceptor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.uflower.user.entity.UserEntity;
import com.uflower.user.service.UserService;
import com.uflower.common.utils.PageUtils;
import com.uflower.common.utils.R;

import javax.annotation.Nullable;


/**
 * 用户表
 *
 * @author bridge
 * @email bridgezrq@163.com
 * @date 2022-01-15 14:57:31
 */
@RestController
@RequestMapping("user/user")
@Api(tags = "用户管理接口")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 列表
     */
    @ApiOperation(value = "获取用户列表",notes = "生成为page")
    @RequestMapping("/list")
    //@RequiresPermissions("user:user:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping({"/info/{id}","/info"})
    //@RequiresPermissions("user:user:info")
    public R info(@Nullable @PathVariable("id") Long id){
        UserEntity user;
        if(id != null){
             user = userService.getById(id);
        }else{
             user = userService.getById(LoginUserInterceptor.loginUser.get().getId());
        }
        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("user:user:save")
    public R save(@RequestBody UserEntity user){
        if (userService.queryByUserName(user.getUsername()) == null){
            if (StringUtils.isEmpty(user.getStatus())) {
                user.setStatus(1);
            }
            if (StringUtils.isEmpty(user.getHeader())) {
                user.setHeader("https://uflower-imges.oss-cn-beijing.aliyuncs.com/default.png");
            }
            if (StringUtils.isEmpty(user.getCreateTime())) {
                user.setCreateTime(new Date());
            }
            userService.save(user);
            return R.ok();
        }else {
            return R.error("用户名被占用");
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("user:user:update")
    public R update(@RequestBody UserEntity user){
        if (StringUtils.isEmpty(user.getUsername())) {
            return R.error("请输入用户名");
        }
        if (StringUtils.isEmpty(user.getPassword())) {
            return R.error("请输入密码");
        }
        if (StringUtils.isEmpty(user.getNickname())) {
            return R.error("请输入昵称");
        }
        if (StringUtils.isEmpty(user.getEmail())) {
            return R.error("请输入邮箱");
        }
        if (StringUtils.isEmpty(user.getBirthday())) {
            return R.error("请输入生日");
        }
        if (StringUtils.isEmpty(user.getGender())) {
            return R.error("请输入性别");
        }
        if (StringUtils.isEmpty(user.getPhone())) {
            return R.error("请输入电话");
        }

        UserEntity userEntity = userService.queryByUserName(user.getUsername());
        if (userEntity == null || userEntity.getId().equals(user.getId())){
            userService.updateById(user);
        }else {
            return R.error("用户名被占用");
        }

        return R.ok();
    }

    /**
     * 修改用户状态
     */
    @ApiOperation(value = "修改用户状态",notes = "修改用户状态")
    @RequestMapping("/updatestatus")
    //@RequiresPermissions("user:user:update")
    public R updateStatus(@RequestBody UserEntity user){
        userService.updateStatus(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("user:user:delete")
    public R delete(@RequestBody Long[] ids){
		userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/saveInfo")
    //@RequiresPermissions("user:user:save")
    public R saveInfo(@RequestBody UserEntity user){
        userService.save(user);

        return R.ok();
    }
}
